﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.Script.Services;
namespace MLM.AdminCP.Pages
{
    [ScriptService]
    public partial class StatisticsManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StartDate.SelectedDate = DateTime.Now.AddDays(-7);
                EndDate.SelectedDate = DateTime.Now;
            }
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetListDate(string startDate, string endDate)
        {
            DateTime _endDate;
            DateTime _startDate;
            if (startDate == "null")
            {
                _startDate = DateTime.Now.AddDays(-7);
            }
            else
            {
                _startDate = DateTime.Parse(startDate);
            }
            if (endDate == "null")
            {
                _endDate = DateTime.Now;
            }
            else
            {
                _endDate = DateTime.Parse(endDate);
            };
            List<listObject> _listObject = new List<listObject>();

            List<DateTime> listDate = new List<DateTime>();
            List<int> _listValue = new List<int>();
            for (DateTime date = _startDate; date <= _endDate; date = date.AddDays(1))
            {

                listDate.Add(date);
            }
            using (var DataContext = new MLMDataContext())
            {
                foreach (var item in listDate)
                {
                    DateTime startdate = new DateTime(item.Year, item.Month, item.Day, 0, 0, 0);
                    DateTime EndDate = new DateTime(item.Year, item.Month, item.Day, 23, 59, 0);

                    int _value = DataContext.Users.Count(p => p.RegistrationDate >= startdate && p.RegistrationDate <= EndDate);
                    _listValue.Add(_value);
                }
            }
            _listObject.Add(new listObject { Date = listDate, value = _listValue });
            JavaScriptSerializer ser = new JavaScriptSerializer();
            return ser.Serialize(_listObject);
        }
    }

    public class listObject
    {
        public List<DateTime> Date { get; set; }
        public List<int> value { get; set; }
    }
}